package com.zhihaoliang.room.base.list

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zhihaoliang.room.databinding.ListItemBinding


/**
 * @author 支豪亮
 * @date 2022/10/24 09:50
 * @des Flow 的 Adapter
 */
class ListAdapter(private val list: List<ListItem>, private val onChannelItemClick: OnListItemClick) :
    RecyclerView.Adapter<ExceptionViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExceptionViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val dataBinding = ListItemBinding.inflate(layoutInflater, parent, false)
        return ExceptionViewHolder(dataBinding)
    }

    override fun onBindViewHolder(holder: ExceptionViewHolder, position: Int) {
        holder.dataBinding.item = list[position]
        holder.dataBinding.itemClick = onChannelItemClick
    }

    override fun getItemCount(): Int = list.size
}

class ExceptionViewHolder(val dataBinding: ListItemBinding) : RecyclerView.ViewHolder(dataBinding.root)

/**
 *  FlowAdapter 的点击
 */
fun interface OnListItemClick {
    fun onItemClick(item: ListItem)
}

/**
 * @des Flow的数据
 */
class ListItem(val title: String, val index: Int)